package com.rc.saas.tenant.controller.banner;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.constant.TenantBannerConstant;
import com.rc.saas.tenant.component.UploadComponent;
import com.rc.saas.tenant.component.UploadImg;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.banner.TenantBanner;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.service.banner.TenantBannerService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.Date;

/**
 * @author frog
 * @date 2019/11/23
 */
@Controller
@RequestMapping("/banner")
public class TenantBannerController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(TenantBannerController.class);

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private UploadImg uploadImg;

    @Autowired
    private TenantBannerService tenantBannerService;

    @Autowired
    private UploadComponent uploadComponent;

    /**
     * 跳转轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/banner/banner";
    }

    /**
     * 跳转轮播图设置页面
     *
     * @return
     */
    @RequestMapping(value = "/bannerIndex", method = RequestMethod.GET)
    public String bannerIndex() {
        return "/templates/banner/tenantBanner";
    }

    /**
     * 返回轮播图列表数据(公众号)
     *
     * @return
     */
    @RequestMapping(value = "/listPageBanner", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageBanner(BaseSearch baseSearch) {
        PageInfo<TenantBanner> pageInfo = tenantBannerService.listPageBanner(baseSearch, "0");
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 返回轮播图列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantBanner", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantBanner(BaseSearch baseSearch) {
        PageInfo<TenantBanner> pageInfo = tenantBannerService.listPageBanner(baseSearch, null);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/addBanner", method = RequestMethod.GET)
    public String addBanner() {
        return "/templates/banner/addBanner";
    }

    /**
     * 跳转增加轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantBanner", method = RequestMethod.GET)
    public String addTenantBanner() {
        return "/templates/banner/addTenantBanner";
    }

    /**
     * 跳转编辑轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/editBanner/{bannerId}", method = RequestMethod.GET)
    public String editBanner(@PathVariable("bannerId") Long bannerId, Model model) {
        TenantBanner tenantBanner = tenantBannerService.selectByPrimaryKey(bannerId);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("tenantBanner", tenantBanner);
        return "/templates/banner/editBanner";
    }

    /**
     * 跳转编辑轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantBanner/{bannerId}", method = RequestMethod.GET)
    public String editTenantBanner(@PathVariable("bannerId") Long bannerId, Model model) {
        TenantBanner tenantBanner = tenantBannerService.selectByPrimaryKey(bannerId);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("tenantBanner", tenantBanner);
        return "/templates/banner/editTenantBanner";
    }

    /**
     * 保存编辑轮播图
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateBanner", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateBanner(TenantBanner tenantBanner, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (!BeanUtil.isValid(tenantBanner.getStatus())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            if (file != null && file.getSize() != 0 && file.getContentType().contains("image")) {
                String imgUrl = uploadComponent.uploadFile(user.getTenantCode(), file);
                if ("0".equals(tenantBanner.getImgType())) {
                    tenantBanner.setAppStartImg(imgUrl);
                    tenantBanner.setBannerType("1");
                } else if ("1".equals(tenantBanner.getImgType())) {
                    tenantBanner.setAppIndexImg(imgUrl);
                    tenantBanner.setBannerType("1");
                } else if ("2".equals(tenantBanner.getImgType())) {
                    tenantBanner.setAppFlashImg(imgUrl);
                    tenantBanner.setBannerType("1");
                } else if ("3".equals(tenantBanner.getImgType())) {
                    tenantBanner.setAppFlashAdImg(imgUrl);
                    tenantBanner.setBannerType("1");
                } else if ("4".equals(tenantBanner.getImgType())) {
                    tenantBanner.setWechatIndexImg(imgUrl);
                    tenantBanner.setBannerType("0");
                } else if ("5".equals(tenantBanner.getImgType())) {
                    tenantBanner.setMiniIndexImg(imgUrl);
                    tenantBanner.setBannerType("2");
                }
            }
          /*  if (file.getSize() == 0 && tenantBanner.getBannerId() == null) {
                return TResponse.FAIL("请上传图片");
            }
            if (file.getSize() != 0) {
                TResponse tResponse = uploadImg.uploadImg(user.getTenantCode(), file);
                if (tResponse.getStatus() == 200) {
                    if ("0".equals(tenantBanner.getImgType())) {
                        tenantBanner.setAppStartImg(imgUrl);
                        tenantBanner.setBannerType("1");
                    } else if ("1".equals(tenantBanner.getImgType())) {
                        tenantBanner.setAppIndexImg(imgUrl);
                        tenantBanner.setBannerType("1");
                    } else if ("2".equals(tenantBanner.getImgType())) {
                        tenantBanner.setAppFlashImg(imgUrl);
                        tenantBanner.setBannerType("1");
                    } else if ("3".equals(tenantBanner.getImgType())) {
                        tenantBanner.setAppFlashAdImg(imgUrl);
                        tenantBanner.setBannerType("1");
                    } else if ("4".equals(tenantBanner.getImgType())) {
                        tenantBanner.setWechatIndexImg(imgUrl);
                        tenantBanner.setBannerType("0");
                    } else if ("5".equals(tenantBanner.getImgType())) {
                        tenantBanner.setMiniIndexImg(imgUrl);
                        tenantBanner.setBannerType("2");
                    }
                } else {
                    return tResponse;
                }
            }*/
            tenantBanner.setLastUpdatedBy(user.getLoginName());
            tenantBanner.setLastUpdatedTime(new Date());
            if (tenantBanner.getBannerId() != null) {
                tenantBannerService.updateByPrimaryKeySelective(tenantBanner);
            } else {
                tenantBanner.setTenantCode(user.getTenantCode());
                tenantBannerService.insertSelective(tenantBanner);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存轮播图失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 删除首页轮播图
     *
     * @return
     */
    @RequestMapping(value = "/deleteBanner", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteBanner(Long bannerId) {
        try {
            tenantBannerService.deleteByPrimaryKey(bannerId);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除轮播图失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转至 app轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/appBanner", method = RequestMethod.GET)
    public String appBanner() {
        return "/templates/banner/appBanner";
    }


    /**
     * app轮播图列表
     *
     * @param baseSearch
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listPageAppBanner", method = RequestMethod.GET)
    public WebPageInfo listPageAppBanner(BaseSearch baseSearch, String imgType) {
        PageInfo<TenantBanner> pageInfo = tenantBannerService.listPageBanner(baseSearch, imgType);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加app轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/addAppBanner", method = RequestMethod.GET)
    public String addAppBanner() {
        return "/templates/banner/addAppBanner";
    }

    /**
     * 跳转编辑轮播图页面
     *
     * @return
     */
    @RequestMapping(value = "/editAppBanner/{bannerId}", method = RequestMethod.GET)
    public String editAppBanner(@PathVariable("bannerId") Long bannerId, Model model) {
        TenantBanner tenantBanner = tenantBannerService.selectByPrimaryKey(bannerId);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("tenantBanner", tenantBanner);
        return "/templates/banner/editAppBanner";
    }


}
